package com.xzl.time.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzl.time.mapper.NoticeMapper;
import com.xzl.time.pojo.Notice;
import com.xzl.time.service.INoticeService;
import com.xzl.time.utils.MyEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.rmi.runtime.Log;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @describe ：
 * @PageLocation:
 * @Auther xzl
 * @Date 2022/1/7 15:34
 */
@Service
@Slf4j
public class NoticeServiceImpl implements INoticeService {
    @Autowired
    private NoticeMapper noticeMapper;
    @Override
    public int insert(Notice notice) {
        int insert = noticeMapper.insert(notice);
        return insert;
    }

    @Override
    public PageInfo<Notice> getNoticeByTypeAndReceiverId(int pageNum, int pageSize, String targetType, String userName) {
        PageHelper.startPage(pageNum,pageSize);
        List<Notice> notices = null;
        //判断是否是评论消息，如果是，就查询评论内容
        if (MyEnum.ARTICLE_COMMENT_.getValue().equals(targetType)){
            notices = noticeMapper.getCommentNoticeByTypeAndReceiverId(targetType, userName);
        }
        //判断是否是点赞或者关注消息，如果是，就查询点赞或关注信息
        else if (MyEnum.ARTICLE_SUBSCRIBE_.getValue().equals(targetType)
        ||MyEnum.ARTICLE_THUMBSUP.getValue().equals(targetType)){
            notices = noticeMapper.getNoticeByTypeAndReceiverId(targetType, userName);
        }
        if (notices!= null && notices.size() > 0) {
            //过滤掉已经查看过了的，获取没查看过的id
            List<String> noticeIds = notices.stream().filter(item -> !item.getState())
                    .map(Notice::getId).collect(Collectors.toList());
            if (noticeIds != null && noticeIds.size() > 0){
                int i = noticeMapper.updNoticeState(noticeIds);
            }
        }
        PageInfo<Notice> pageInfo = new PageInfo<>(notices);
        return pageInfo;
    }

}
